package com.hhhn.wk.widget.picture_viewer;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.GlideDrawableImageViewTarget;
import com.hhhn.wk.R;
import com.hhhn.wk.base.BaseFragment;
import com.hhhn.wk.utils.Constants;
import com.hhhn.wk.widget.picture_viewer.pv_02.PhotoViewAttacher;


//import com.nostra13.universalimageloader.core.ImageLoader;
//import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
//import com.nostra13.universalimageloader.core.assist.FailReason;
//import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;

/**
 * 单张图片显示Fragment
 */
public class ImageDetailFragment extends BaseFragment {
	private static final String TAG = "ImageDetailFragment";
	private String mImageUrl;
	private ImageView mImageView;
//	private ProgressBar progressBar;
	private PhotoViewAttacher mAttacher;
	static Bundle args;

	public static ImageDetailFragment newInstance(String imageUrl) {
		final ImageDetailFragment f = new ImageDetailFragment();

		args = new Bundle();

		args.putString("url", Constants.HTTPIMG+imageUrl);/*注意这里加上HTTP协议的头*/
//		args.putString("url", "http://image.baidu.com/search/down?tn=download&word=download&ie=utf8&fr=detail&url=http%3A%2F%2F5b0988e595225.cdn.sohucs.com%2Fimages%2F20170706%2F45d963a8d2814596a130f03ceb2dbebd.png&thumburl=http%3A%2F%2Fb.hiphotos.baidu.com%2Fimage%2Fh%253D200%2Fsign%3D8b1f7555ddca7bcb627bc02f8e086b3f%2F7dd98d1001e93901d82ec1a871ec54e736d1967c.jpg");/*注意这里加上HTTP协议的头*/
		Log.d(TAG, "newInstance: "+Constants.HTTPIMG+imageUrl);
		Log.d(TAG, "newInstance: "+args.getString("url"));
		f.setArguments(args);

		return f;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		mImageUrl = getArguments() != null ? getArguments().getString("url") : null;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		final View v = inflater.inflate(R.layout.picyure_viewer_image_pager_fragment, container, false);
		mImageView = (ImageView) v.findViewById(R.id.image);

		mAttacher = new PhotoViewAttacher(mImageView);

		mAttacher.setOnPhotoTapListener(new PhotoViewAttacher.OnPhotoTapListener() {

			@Override
			public void onPhotoTap(View arg0, float arg1, float arg2) {
				getActivity().finish();
			}
		});

//		progressBar = (ProgressBar) v.findViewById(R.id.loading);



		return v;
	}

	@Override
	protected void initView(View mRootView) {

	}

	@Override
	protected void initData() {

	}

	@Override
	protected void initClick() {

	}

	@Override
	protected int setLayoutResouceId() {
		return 0;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
//		ImageLoader.getInstance().init(ImageLoaderConfiguration.createDefault(getActivity()));
		/***
		 * 根据它【mImageUrl】判断是图片还是视频
		 */
		String p_end1=".jpeg";
		String p_end2=".png";
		String p_end3=".jpg";
		String p_end4=".JPEG";
		String p_end5=".PNG";
		String p_end6=".JPG";

		String v_end1=".avi";
		String v_end2=".wmv";
		String v_end3=".mp4";
		String v_end4=".flv";
		String v_end5=".rmvb";
		String v_end6=".3gp";
//		if (mImageUrl.endsWith(p_end1) || mImageUrl.endsWith(p_end2)||mImageUrl.endsWith(p_end3)
//				||mImageUrl.endsWith(p_end4) || mImageUrl.endsWith(p_end5)||mImageUrl.endsWith(p_end6)){
//			Log.d(TAG, "图片图片图片图片图片图片图片图片: "+mImageUrl);
//			goPicture();
//		}else if (mImageUrl.endsWith(v_end1) || mImageUrl.endsWith(v_end2)||mImageUrl.endsWith(v_end3)
//				||mImageUrl.endsWith(v_end4)||mImageUrl.endsWith(v_end5)||mImageUrl.endsWith(v_end6)){
//			Log.d(TAG, "视频视频视频视频视频视频视频: ");
//		}
		goPicture();
	}


	/**
	 * 加载图片
	 */
	private void goPicture(){
//		ImageLoader.getInstance().init(ImageLoaderConfiguration.createDefault(getActivity()));

//		ImageLoader.getInstance().displayImage(mImageUrl, mImageView, new SimpleImageLoadingListener() {
//			@Override
//			public void onLoadingStarted(String imageUri, View view) {
//				progressBar.setVisibility(View.VISIBLE);
//			}
//			@Override
//			public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
//				String message = null;
//				switch (failReason.getType()) {
//					case IO_ERROR:
//						message = "下载错误";
//						break;
//					case DECODING_ERROR:
//						message = "图片无法显示";
//						break;
//					case NETWORK_DENIED:
//						message = "网络有问题，无法下载";
//						break;
//					case OUT_OF_MEMORY:
//						message = "图片太大无法显示";
//						break;
//					case UNKNOWN:
//						message = "未知的错误";
//						break;
//				}
//				Toast.makeText(getActivity(), message, Toast.LENGTH_SHORT).show();
//				progressBar.setVisibility(View.GONE);
//			}
//			@Override
//			public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
//				progressBar.setVisibility(View.GONE);
//				mAttacher.update();
//			}
//		});
		mActivity.showLoad();//显示loading效果
		Glide.with(mActivity)
				.load(mImageUrl)
				.centerCrop()
				.error(R.mipmap.logo)
				.crossFade()
				.into(new GlideDrawableImageViewTarget(mImageView) {
					@Override
					public void onResourceReady(GlideDrawable drawable, GlideAnimation anim) {
						super.onResourceReady(drawable, anim);
						mActivity.dismissLoad();//关闭loading效果
					}});
	}












}
